package org.hs.phd.odi.tracking.ui;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.util.List;

import org.hs.phd.common.frameviewer.FrameViewer;
import org.hs.phd.common.frameviewer.ViewerLayer;
import org.hs.phd.odi.tracking.tracker.Track;

public class TracksLayer implements ViewerLayer {

	private List<Track> tracks;
	private FrameViewer viewer;
	private boolean isEnabled;
	
	

	public void setTracks(List<Track> tracks) {
		this.tracks = tracks;
		this.isEnabled = true;
		viewer.repaint();
	}



	@Override
	public void setViewer(FrameViewer viewer) {
		this.viewer = viewer;
	}



	@Override
	public void mouseClicked(MouseEvent e) {
		//do nothing
	}



	@Override
	public void mouseEntered(MouseEvent e) {
		//do nothing
	}



	@Override
	public void mouseExited(MouseEvent e) {
		//do nothing
	}



	@Override
	public void mousePressed(MouseEvent e) {
		//do nothing
	}



	@Override
	public void mouseReleased(MouseEvent e) {
		//do nothing
	}



	@Override
	public void mouseDragged(MouseEvent e) {
		//do nothing
	}



	@Override
	public void mouseMoved(MouseEvent e) {
		//do nothing
	}



	@Override
	public void paint(Graphics g) {
		if(tracks == null) return;
		if(!isEnabled) return;
		
		
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setStroke(new BasicStroke(2));
		for( Track track : tracks ){
			if(track == null ){
				System.err.println("Warning : track is null");
				continue;
			}
			
			if(track.isEmpty()){
				continue;
			}
			
			g.setColor( track.getColor() );
			g.drawPolygon(  viewer.getTransformation().modelToView(track.getWindowAsPolygon()));
		}
	}



	@Override
	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}



	@Override
	public boolean isEnabled() {
		return isEnabled;
	}

}
